package com.eos.inter.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eos.common.tool.DaoAccessObjectPoolContextFactory;
import com.eos.orm.mybatis.dao.CustGuaranteeMonthReportMapper;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReport;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReportExample;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReportExample.Criteria;
import com.hf.utils.StringUtil;

/**
 * 担保手续费的结算阿里回调接口
 * 
 * @author sz
 * 
 */
public class CustGuaranteeMonthReportServlet extends BaseHttpServlet {

	private static final long serialVersionUID = 1L;
	private CustGuaranteeMonthReportMapper mapper = (CustGuaranteeMonthReportMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject(
					"CustGuaranteeMonthReportMapper");

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CustGuaranteeMonthReport custGuaranteeMonthReport = new CustGuaranteeMonthReport();// 添加数据的对象
		custGuaranteeMonthReport.setTaobaoOrderId(StringUtil
				.NullToString(request.getParameter("taobao_order_id")));

		custGuaranteeMonthReport.setBackTime(StringUtil.stringDate(new Date()));
		custGuaranteeMonthReport.setBackStatus(StringUtil.NullToString(request
				.getParameter("back_status")));// 0表示核对正常 1表示核对异常

		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			if (identification(custGuaranteeMonthReport.getTaobaoOrderId(),
					custGuaranteeMonthReport.getBackStatus())) {
				// 失败返回1
				out.write("taobao_order_id或back_status为空".toString().getBytes(
						"utf-8"));
				return;
			}
			CustGuaranteeMonthReportExample example = new CustGuaranteeMonthReportExample();
			Criteria criteria = example.createCriteria();
			criteria.andTaobaoOrderIdEqualTo(custGuaranteeMonthReport
					.getTaobaoOrderId());
			if (mapper.updateByExampleSelective(custGuaranteeMonthReport,
					example) == 0) {
				// 成功返回0
				out.write("0".toString().getBytes("utf-8"));
			} else {
				// 失败返回1
				out.write("1".toString().getBytes("utf-8"));
			}
		} catch (Exception e) {
			log
					.error("com.eos.inter.servlet.CustGuaranteeMonthReportServlet.doPost throws exception:"
							+ e.getMessage());
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}
}
